package com.lmc.edrive.travel;

import java.util.ArrayList;
import java.util.List;

import com.lmc.edrive.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lmc.edrive.adapter.AlertAdapter;
import com.lmc.edrive.adapter.VehicleAdapter;
import com.lmc.edrive.base.EDriveBaseActivity;
import com.lmc.edrive.comon.HttpASync;
import com.lmc.edrive.comon.HttpASync.OnRequest;
import com.lmc.edrive.constant.AppConstant;
import com.lmc.edrive.data.Vehicle;
import com.lmc.edrive.home.HomeScreenActivity;

import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.os.Handler;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class VehicleActivity extends EDriveBaseActivity implements	OnClickListener ,OnRequest{
	
	private ListView vehicleList;
	private VehicleAdapter adapter;

	private TextView vehiclename;
	private TextView modelname;
	private Spinner modelyear;
	private TextView mileage;
	private ImageView addvehicle;
	private ImageView deletevehicle;
	private ImageView editvehicle;
	private List<Vehicle> vehicleListData; 
	public static final int DELETE_VEHICLE=1;
	private Context ctx;
	private Handler handler=new Handler(){
		

		public void handleMessage(android.os.Message msg) {
			if(msg.what==VehicleActivity.DELETE_VEHICLE) {
				int pos = msg.arg1;
				Toast.makeText(ctx, "Deleting....", Toast.LENGTH_SHORT).show();
				SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
				String uName = pref.getString("username", null);
				HttpASync async = new HttpASync();
				String url=AppConstant.DELETE_VEHICLE+"&emailid="+uName+"&vhname="+vehicleListData.get(pos).getVehicleName();
				async.execute(url);
				finish();
			}
		};
	};
	
		


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ctx=this;
		vehiclename=(EditText)findViewById(R.id.vehiclename);
		modelyear=(Spinner)findViewById(R.id.spinner1);
		mileage=(TextView)findViewById(R.id.textView4);
		
		
		
		setContentView(R.layout.vehicle);

		addvehicle = (ImageView) findViewById(R.id.addvehicle);
	
		vehicleList=(ListView) findViewById(R.id.vehiclelist);
		
		
		deletevehicle=(ImageView) findViewById(R.id.deletevehicle);
		
		addvehicle.setOnClickListener(this);
		
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.addvehicle) {
			Intent i = new Intent(ctx, AddVehicle.class);
			startActivity(i);
		}		}

	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
		String uName = pref.getString("username", null);
		HttpASync async = new HttpASync();
		String url=AppConstant.ADD_VEHICLE+"?emailid="+uName+"&method=get";
		async.setOnRequestListener(this);
		async.execute(url);
		showProgress("Loading your vehicles...");
	}

	@Override
	public void onResponseReceived(String response) {
		
		Gson gson = new Gson();
		
		vehicleListData = gson.fromJson(response,new TypeToken<List<Vehicle>>(){}.getType());
		for (Vehicle vehicle : vehicleListData) {
			System.out.println("Vehicle : "+vehicle.getVehicleName());
		}
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				
				adapter=new VehicleAdapter(ctx,handler,vehicleListData);
				vehicleList.setAdapter(adapter);
				hideProgress();
			}
		});
		
	}
}
